
#include <BMainWindow>
#include <BViewer>
#include <BAnimation>
#include <BSampler>
#include <BButton>
#include <BLabel>
#include <BImage>
#include <BReadWrite>
#include <BRefer>
#include <BHBoxLayout>
#include <BApplication>

using namespace BWE;

typedef BKeyframeX<BPointd>	PointKeyframe;
class MoveChannel : public BChannel
{
public:
	MoveChannel()
	{
		_sampler = new BLinearSampler<BPointd>();
	}

	void addKeyframe(float time, const BPointd& point)
	{
		_keyframes.append(BKeyframeX<BPointd>(time, point));
	}

	virtual int keyframeCount() const
	{
		return _keyframes.size();
	}
	virtual BKeyframe& keyframe(int index)
	{
		return _keyframes(index);
	}
	virtual bool accept(BObject* object)
	{
		BWidget* widget = dynamic_cast<BWidget*>(object);
		if (widget)
			return true;
		return false;
	}
	virtual void apply(BObject* object)
	{
		BWidget* widget = (BWidget*)object;
		if (widget)
		{
			widget->setPos(_point);
		}
	}
	virtual void update(double time)
	{
		_point = _sampler->lerp(_keyframes, time);
	}

private:
	BPointd						_point;
	BSampler<BPointd>*			_sampler;
	BArray<BKeyframeX<BPointd>>	_keyframes;
};

int main(int argc, int argv)
{
	BApplication app;

	MoveChannel* channel = new MoveChannel();
	channel->setTargetName("label");
	channel->addKeyframe(0, BPointd(20, 20));
	channel->addKeyframe(2, BPointd(100, 300));
	channel->addKeyframe(3, BPointd(160, 200));
	channel->addKeyframe(4, BPointd(20, 20));

	BAnimation* animation = new BAnimation();
	animation->setDuration(4.1);
	animation->addChannel(channel);
	animation->start();

	BImage* image = Read<BImage>("data/ui/neon/neon_004.png");

	BLabel* label = new BLabel(image);
	label->setName("label");
	label->setAnimation(animation);

	BMainWindow* mw = new BMainWindow();
	mw->addChild(label);
	mw->show();

	return app.execute();
}